import utils from './utils/index.js'
import Game from './game/index.js'

// 读取的总配置文件
const SETTING_PATH = 'settings.json'

class GameData {
  constructor () {}

  // 初始化
  async init () {
    const data = await utils.laya.loadJSON(SETTING_PATH)

    this.settings = {}
    for (let key in data) {
      this.settings[key] = data[key]
    }

    // 地图列表
    this.maps = {}
  }

  // 读取地图数据
  async loadMap (mapId) {
    if (this.maps[mapId]) {
      // 已从文件获取过地图，则不用重复读取
      return this.maps[mapId]
    }

    const mapPath = 'maps/map_' + mapId + '.json'
    const map = await utils.laya.loadJSON(mapPath)
    // 创建地图并且加载地图资源
    this.maps[mapId] = new Game.Map(map)
    await this.maps[mapId].initSrc()
    return this.maps[mapId]
  }
}

let gameData = new GameData()

export default gameData